package com.example.e_app;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_name;
    private RadioButton rb_man;
    private RadioButton rb_wuman;
    private CheckBox cb_swim;
    private CheckBox cb_read;
    private CheckBox cb_sing;
    private Button button;
    private TextView tv_show;
    private RadioGroup sexs;
    private LinearLayout habbys;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        et_name = (EditText) findViewById(R.id.et_name);
        rb_man = (RadioButton) findViewById(R.id.rb_man);
        rb_wuman = (RadioButton) findViewById(R.id.rb_wuman);
        cb_swim = (CheckBox) findViewById(R.id.cb_swim);
        cb_read = (CheckBox) findViewById(R.id.cb_read);
        cb_sing = (CheckBox) findViewById(R.id.cb_sing);
        button = (Button) findViewById(R.id.button);
        tv_show = (TextView) findViewById(R.id.tv_show);

        button.setOnClickListener(this);
        sexs = (RadioGroup) findViewById(R.id.sexs);
        sexs.setOnClickListener(this);
        habbys = (LinearLayout) findViewById(R.id.habbys);
        habbys.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                submit();
                break;
        }
    }

    private void submit() {
        // validate

        String info="";
        String name = et_name.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "姓名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        info=info+"姓名："+name+"\n";

        /*String sex = "";
        if (rb_man.isChecked()) {
            sex = rb_man.getText().toString().trim();
        } else if (rb_wuman.isChecked()) {
            sex = rb_wuman.getText().toString().trim();
        } else {
            Toast.makeText(this, "性别不能为空", Toast.LENGTH_SHORT).show();
            return;
        }*/

        String sex = "";
        int count = sexs.getChildCount();
        for (int i = 0; i < count; i++) {
            RadioButton radioButton = (RadioButton) sexs.getChildAt(i);
            if (radioButton.isChecked()) {
                sex = radioButton.getText().toString();
                break;
            }
        }
        if(sex.length()<=0){
            Toast.makeText(this, "性别不能为空", Toast.LENGTH_SHORT).show();
            return;
        }else{
            info+="性别："+sex+"\n";
        }

        String str_habbys="";
        int count2 = habbys.getChildCount();
        for (int i = 0; i < count2; i++) {
            CheckBox checkBox = (CheckBox) habbys.getChildAt(i);
            if (checkBox.isChecked()) {
                str_habbys+=checkBox.getText().toString()+" ";

            }
        }
        info = info + "爱好："+str_habbys+"\n";


        tv_show.setText(info);



    }
}
